<?php

class SimilarityControl extends CI_Controller
{
	function __construct() 
	{		
		parent::__construct();
		$this->load->model('ptdomainmodel'); //ntar diganti
		$this->load->model('ptgenremodel');
		$this->load->model('similargenremodel');
		$this->load->model('genredominanmodel');
		$this->load->model('usiaUserModel');
	}
	
	
	function similarityGenre(){ //perlu parameter?
		$session_data = $this->session->userdata('logged_in');
     	$userid = $session_data['userid'];
     	$username = $session_data['username'];
     	$gender = $session_data['gender'];
     	$usia = $this->usiaUserModel->getUsia($userid)->result();
		if (count($usia) > 1) {
     		for ($j2 = 0; $j2 < count($usia); $j2++) {
     			$age[$j2] = $usia[$j2]->USIAID;
     		}
     		$usiaid = max($age);
     	} else {
     		$usiaid = $usia[0]->USIAID;
     	}
		$ptuser = $this->ptdomainmodel->getSkorDomain($userid,$usiaid)->result(); //2 = useid, 1 = usiaid
		$ptgender = $this->ptgenremodel->getPTGender($gender)->result();
		for ($j = 0; $j < sizeof($ptgender) ; $j++) {
			$neuro[$j] = $ptgender[$j]->SKORNEURO;
			$extra[$j] = $ptgender[$j]->SKOREXTRA;
			$open[$j] = $ptgender[$j]->SKOROPEN;
			$agree[$j] = $ptgender[$j]->SKORAGREE;
			$cons[$j] = $ptgender[$j]->SKORCONSCIENT;
		}
		$max = array( 
			'neuro' => max($neuro),
			'extra' => max($extra),
			'open' => max($open),
			'agree' => max($agree),
			'cons' => max($cons)
		); //mencari nilai max dari setiap domain ptgenre
		$min = array( 
			'neuro' => min($neuro),
			'extra' => min($extra),
			'open' => min($open),
			'agree' => min($agree),
			'cons' => min($cons)
		); //mencari nilai min dari setiap domain ptgenre
//		$show['konversi']=array();
		for($i=0;$i<sizeof($max);$i++){
			$range[$i] = current($max) - current($min);
			$konversi[$i] = ($ptuser[$i]->nilaidomain - 99) * ($range[$i] / 98) + current($max);
			next($max);
			next($min);
//			$show['konversi'][$i] = $konversi[$i];
		} //menghitung skor konversi dari skor domain user
		$show['konversi']=$konversi; //mengambil skor konversi user untuk ditampilkan
		$meanuser = array_sum($konversi) / count($konversi); 
		$genre = $this->ptgenremodel->getGenre()->result();
		for($i=0;$i<sizeof($genre);$i++){
			$ptgenre[$i] = $this->ptgenremodel->getPTGenre($genre[$i]->GENREID,$gender)->result();
			$show['ptgenre'][$i] = $ptgenre[$i];
		}
		
		for($i=0;$i<sizeof($genre);$i++){
			$meangenre[$i] = ($ptgenre[$i][0]->SKORNEURO+$ptgenre[$i][0]->SKOREXTRA+
								$ptgenre[$i][0]->SKOROPEN+$ptgenre[$i][0]->SKORAGREE+
								$ptgenre[$i][0]->SKORCONSCIENT)/count($ptuser); //menghitung rata-rata dari PT genre
			$sumdomain[$i] = (($konversi[0]-$meanuser)*($ptgenre[$i][0]->SKORNEURO-$meangenre[$i]))+
								(($konversi[1]-$meanuser)*($ptgenre[$i][0]->SKOREXTRA-$meangenre[$i]))+
								(($konversi[2]-$meanuser)*($ptgenre[$i][0]->SKOROPEN-$meangenre[$i]))+
								(($konversi[3]-$meanuser)*($ptgenre[$i][0]->SKORAGREE-$meangenre[$i]))+
								(($konversi[4]-$meanuser)*($ptgenre[$i][0]->SKORCONSCIENT-$meangenre[$i]));
			for ($j = 0; $j < sizeof($konversi); $j++) {
				$user[$j] = pow(($konversi[$j]-$meanuser), 2);
			}
			$sgenre[1] = pow(($ptgenre[$i][0]->SKORNEURO-$meangenre[$i]), 2);
			$sgenre[2] = pow(($ptgenre[$i][0]->SKOREXTRA-$meangenre[$i]), 2);
			$sgenre[3] = pow(($ptgenre[$i][0]->SKOROPEN-$meangenre[$i]), 2);
			$sgenre[4] = pow(($ptgenre[$i][0]->SKORAGREE-$meangenre[$i]), 2);
			$sgenre[5] = pow(($ptgenre[$i][0]->SKORCONSCIENT-$meangenre[$i]), 2);
			
			$sumakardomain[$i] = sqrt(array_sum($user))*sqrt(array_sum($sgenre)) ;			
			$similar[$i] = $sumdomain[$i] / $sumakardomain[$i];
			$genres[$i] = $this->genredominanmodel->getGenre($genre[$i]->GENREID)->result();
			$show['genre'][$i] = $genres[$i];
		}
		$show['similar'] = $similar;
		$sim = max($similar);
		$ig = array_keys($similar, $sim);
//		$gen = $this->genredominanmodel->getGenre($genre[$ig[0]]->GENREID)->result();
		$this->similargenremodel->saveSimilarValue($userid, $usiaid, $genre[$ig[0]]->GENREID, $sim);
//		echo $show['konversi'][0];
//		echo $ptgenre[1][0]->SKORNEURO;
//		echo $genre[0]->GENREID;
//		echo "user : ".$meanuser;
//		echo "genre : ".$meangenre[1];
		
		$this->load->view('tessimilar',$show);
		if ($this->input->post('submit')) {
			redirect('recommendationcontrol/recommendation', 'refresh');
		}
		
	}
	
}

?>